package ar.com.spsolutions.splilabrary.view.version2.components;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.model.util.ListModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.com.spsolutions.splibrary.general.RoleConverterService;
import ar.com.spsolutions.splilabrary.view.annotations.RenderAllowed;
import ar.com.spsolutions.splilabrary.view.application.WebSession;
import ar.com.spsolutions.splilabrary.view.pages.BasePage;

public class AbstractNavigatorBar extends Panel {

	private List<Class<? extends BasePage>> menues;

	@SpringBean
	private RoleConverterService roleConverterService;
	private static final long serialVersionUID = 2847494284382014641L;

	public AbstractNavigatorBar(final List<Class<? extends BasePage>> menues) {
		super("navigatorBar");
		this.menues = menues;
		this.createBar();
	}

	private void createBar() {
		this.add(new ListView<Class<? extends BasePage>>("menuRow",
				new ListModel<Class<? extends BasePage>>(this.getMenues())) {
			private static final long serialVersionUID = 8663388719655979106L;

			@Override
			protected void populateItem(
					final ListItem<Class<? extends BasePage>> item) {

				final Link<String> link = new Link<String>("menuItem") {
					private static final long serialVersionUID = 1994280240752618745L;

					@Override
					public void onClick() {
						this.setResponsePage(item.getModelObject());
					}
				};
				final String pageName = item.getModelObject().getSimpleName();
				link.add(new Label("menuText", new ResourceModel(pageName, item
						.getModelObject().getSimpleName())));
				item.add(link);
			}
		});

	}

	private List<Class<? extends BasePage>> getMenues() {
		final List<Class<? extends BasePage>> filteredMenues = new ArrayList<Class<? extends BasePage>>();
		for (final Class<? extends BasePage> page : this.menues) {
			if (this.isRenderAllowed(page)) {
				filteredMenues.add(page);
			}
		}
		return filteredMenues;
	}

	private boolean isRenderAllowed(final Class<? extends BasePage> page) {
		final WebSession session = (WebSession) this.getSession();
		final Roles roles = this.getRoleConverterService().getRoleFor(
				session.getUser());
		return roles.hasAnyRole(this.getRenderRoles(page));
	}

	private Roles getRenderRoles(final Class<? extends BasePage> page) {
		final RenderAllowed annotation = page.getAnnotation(RenderAllowed.class);
		if (annotation == null) {
			return new Roles(RoleConverterService.ROLE_GUEST);
		}
		return new Roles(annotation.value());
	}

	@Override
	protected void onComponentTag(final ComponentTag tag) {
		super.onComponentTag(tag);
		tag.setId("nemu");
	}

	// --accessors--

	public void setMenues(final List<Class<? extends BasePage>> menues) {
		this.menues = menues;
	}

	public RoleConverterService getRoleConverterService() {
		return this.roleConverterService;
	}

	public void setRoleConverterService(
			final RoleConverterService roleConverterService) {
		this.roleConverterService = roleConverterService;
	}

}
